package com.yurun.controller;

import com.yurun.common.Result;
import com.yurun.entity.Classes;
import com.yurun.service.ClassesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 宫静雨
 */
@RestController
public class ClassesController {

    @Resource
    private ClassesService classesService;

    @PostMapping("/YR100204")
    public Result add(@RequestBody Classes classes) {
        if (Objects.isNull(classes.getClassId()) ||
                Objects.isNull(classes.getClassName())) {
            throw new IllegalArgumentException();
        }
        return classesService.save(classes) ?
                Result.res("000000", "Succeeded") :
                Result.res("000001", "Fail");
    }

    @PostMapping("/YR100203")
    public Result modify(@RequestBody Classes classes) {
        if (Objects.isNull(classes.getClassId())) {
            throw new IllegalArgumentException();
        }
        return classesService.updateById(classes) ?
                Result.res("000000", "Succeeded") :
                Result.res("000001", "Fail");
    }

    @GetMapping("/YR100202")
    public Result getById(@RequestParam Integer classId) {
        final Classes classes = classesService.getById(classId);
        return Result.res("000000", "Succeeded")
                .setData(classes);
    }

    @GetMapping("/YR100201")
    public Result list(@RequestParam(required = false) List<Integer> classId,
                       @RequestParam(required = false) String className) {
        final List<Classes> list = classesService.getByIdsOrClassName(classId, className);
        return Result.res("000000", "Succeeded")
                .setData(list);
    }
}
